import {
	RECORD_TOKENACCESS,
	RECORD_TOKEN,
	RECORD_ISBACK,
	RECORD_ISINLAND,
	RECORD_CABIN,
	RECORD_CITY,
	RECORD_DATE,
	RECORD_FLIGHT,
	RECORD_INTERRT,
	RECORD_PRICE,
	RECORD_ADULTCOUNT,
	RECORD_PASSENGERLIST,
	RECORD_EDITPASSENGER,
	RECORD_PHONENUMBER,
	RECORD_CONTACTUSER,
	RECORD_INSURANCE,	
	RECORD_ISREIMBURSEMENT,
	RECORD_REIMBURSEMENTMESS,
	RECORD_ORDERDETAIL,
	RECORD_LAKALAUSER,
	
} from './mutation-types.js'

import {
	setStore,
	getStore,
} from '../config/util'

export default {
	//验证登录
	[RECORD_TOKENACCESS](state,val){
		state.tokenAccess.Sign = val.Sign;
		state.tokenAccess.ResData = val.ResData;
		state.tokenAccess.AccessChannel = val.AccessChannel;
	},
	// 记录TOKEN值
	[RECORD_TOKEN](state,token) {		
		//初始化token值
		if(!token.type){
			setStore('token',token)	
		}else{
			//更新token值
			if(token.val){
				setStore('token',token.val)	
			}			
		}
		state.token = getStore('token');
	},
	// 拉卡拉用户信息
	[RECORD_LAKALAUSER](state,val) {
		state.contactUser = val.name;
		state.phoneNumber = val.mobile;
	},
	// 记录行程类型
	[RECORD_ISBACK](state,isBack) {
		state.isBack = isBack;
	},
	//记录是否是国内行程
	[RECORD_ISINLAND](state, isInland) {
		state.isInland = isInland;
	},
	//记录舱位状态
	[RECORD_CABIN](state, cabin) {
		state.cabin = cabin;
	},
	//记录搜索城市
	[RECORD_CITY](state, value) {
		if(typeof value=="string"){
			if(value=="ex"){
				var city = JSON.parse(JSON.stringify(state.city));
				state.city.depCity = city.arrCity;
				state.city.arrCity = city.depCity;
				state.city.depCityCode = city.arrCityCode;
				state.city.arrCityCode = city.depCityCode;
			}else{
				state.city.type = value;
			}			
		}else{
			if(state.city.type=="dep"){				
				state.city.depCity = value.name;
				state.city.depCityCode = value.sanzima;
			}
			if(state.city.type=="arr"){
				state.city.arrCity = value.name;
				state.city.arrCityCode = value.sanzima;
			}
		}		
	},
	//记录搜索日期
	[RECORD_DATE](state, value) {
		if(typeof value=="string"){
			state.Date.type = value;
		}else{
			if(state.Date.type=="dep"){				
				state.Date.depDate = value.mdVal;
				state.Date.postDepDate = value.postVal;
			}
			if(state.Date.type=="arr"){
				state.Date.arrDate = value.mdVal;
				state.Date.postArrDate = value.postVal;
			}
		}	
	},
	//记录所选航班信息
	[RECORD_FLIGHT](state, data) {
		if(data.type=="inlandGo"){
			//重置国内航班数据，避免重新选择航班时无法选择回程航班，
			state.flight.inland = [data.value];
		}
		if(data.type=="inlandBack"){
			state.flight.inland[1] = data.value;
		}
		if(data.type=="inter"){			
			state.flight.inter = data.value;			
		}
		//删除国内返程信息
		if(data.type=="deleteBackRouteData"){
			state.flight.inland.splice(1,1);
		}
		//记录国内往返去程的舱位信息
		if(data.type=="inlandRTCabin_F"){
			state.flight.inlandRTCabin_F = data.value;
			state.flight.RTCabin_detail[0] = data.value;
		}
		//记录国内往返程的舱位信息
		if(data.type=="RTCabin"){
			state.flight.RTCabin = data.value;
			state.flight.RTCabin_detail[1] = state.flight.inland[1].prices[data.index];
		}
		//记录 国内单程的或者国际的单程和往返的舱位信息
		if(data.type=="OWCabin"){
			state.flight.OWCabin = data.value;
		}
	},
	//国际往返所有的数据
	[RECORD_INTERRT](state,data){
		if(data.type=="all"){
			state.interRT.data = data.value;
			state.interRT.index = 0;
		}
		if(data.type=="back"){
			state.interRT.index = 1;
		}
		if(data.type=="go"){
			state.interRT.index = 0;
		}
	},
	//验价结果
	[RECORD_PRICE](state,val){
		if(val.type){
			if(val.type=="dep"){
				state.price = [val.result];
			}
			if(val.type=="arr"){
				state.price[1] = val.result;
			}
		}else{
			state.price = [val];
		}
	},
	//成人数量
	[RECORD_ADULTCOUNT](state,val){
		state.adultCount = val;
	},
	//是否报销
	[RECORD_ISREIMBURSEMENT](state,val){
		state.isReimbursement = val;
	},
	//选中的乘客
	[RECORD_PASSENGERLIST](state,data){	
		//从接口中获取全部乘机人信息
		if(data.type=="getAll"){
			var oldVal = [];
			for(var i=0;i<state.passengerList.length;i++){
				oldVal.push(state.passengerList[i].id);
			}
			for(var j=0;j<data.passenger.length;j++){
				var id = data.passenger[j].id;
				var result = oldVal.indexOf(id);
				if(result==-1){
					state.passengerList.push(data.passenger[j]);
				}
			}
		}
		//修改乘机人信息
		if(data.type=="edit"){
			var target = data.passenger.id;
			for(var j=0;j<state.passengerList.length;j++){
				var id = state.passengerList[j].id;
				if(id==target){
					var index = state.passengerList[j].checked;					
					var json = JSON.parse(JSON.stringify(data.passenger));
					json.checked = index;
					state.passengerList.splice(j,1,json);
				}
			}
		}
		//清空乘机人列表
		if(data=="clear"){
			state.passengerList = [];
		}
	},
	//要修改的乘客信息
	[RECORD_EDITPASSENGER](state,data){		
		state.editPassenger = data;
	},
	//存入选中的保险信息
	[RECORD_INSURANCE](state,data){
		if(data.type){
			//保险选择与乘机人列表联动
			if(data.type=="delete"){	//删除联动			
				var deleteID = data.id;
				for(var i=0;i<state.insurance.length;i++){
					for(var j=0;j<state.insurance[i].name.length;j++){
						var id = state.insurance[i].name[j].id;						
						if(id==deleteID){
							state.insurance[i].name.splice(j,1);										
						}
					}
				}
			}
			if(data.type=="edit"){     //编辑联动
				var editID = data.passenger.id;
				for(var i=0;i<state.insurance.length;i++){
					for(var j=0;j<state.insurance[i].name.length;j++){
						var id = state.insurance[i].name[j].id;						
						if(id==editID){
							var obj = JSON.parse(JSON.stringify(data.passenger));
							var old = JSON.parse(JSON.stringify(state.insurance[i].name[j]));
							obj.isBuy = old.isBuy;
							state.insurance[i].name.splice(j,1,obj);	
						}
					}
				}
			}
			
			if(data.type=="clear"){      //清除
				state.insurance = [];
			}
		}else{                        //保险页面点击完成
			state.insurance = data;
		}
	},
	//下单时填写的手机号码
	[RECORD_PHONENUMBER](state,data){		
		state.phoneNumber = data;
	},
	//下单时填写的联系人
	[RECORD_CONTACTUSER](state,data){		
		state.contactUser = data;
	},
	//报销的信息
	[RECORD_REIMBURSEMENTMESS](state,data){
		
	},
	//订单详情
	[RECORD_ORDERDETAIL](state,data){
		state.orderDetail = data;
	},
}